if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_insertAuthorAndWriting]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_insertAuthorAndWriting]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE sp_insertAuthorAndWriting
@Email varchar(50),
@FirstName varchar(30),
@LastName varchar(30),
@Address varchar(200),
@City varchar(30),
@State char(2),
@Country varchar(30),
@ZipCode varchar(10),
@Phone varchar(15),
@Fax varchar(15),
@Church varchar(50),
@RecommendBy varchar(50),

@Title varchar(200),
@CategoryID int,
@SourcePostID int,

@ReturnID int=-1 output
AS

insert into Author (Email, FirstName, LastName, Address, City, State, Country, ZipCode, Phone, Fax, Church, RecommendBy) 
           values (@Email, @FirstName, @LastName, @Address, @City, @State, @Country, @ZipCode, @Phone, @Fax, @Church, @RecommendBy);

declare @AuthorID int;
select @AuthorID = AuthorID from Author where Email = @Email;

declare @ReceiveDate datetime;
set @ReceiveDate = getdate();

insert into Writing (AuthorID, Title, CategoryID, SourcePostID, ReceiveDate) 
           values (@AuthorID, @Title, @CategoryID, @SourcePostID, @ReceiveDate);

SELECT @ReturnID=@AuthorID;
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

